package xyz.weizhi.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @Author milian
 * @Date 2021/9/13 0013 19:44
 */
public class jdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Bean
    public DataSource getDataSource(){
        DruidDataSource DataSource = new DruidDataSource();
        DataSource.setDriverClassName(driver);
        DataSource.setUrl(url);
        DataSource.setUsername(username);
        DataSource.setPassword(password);
        return DataSource;
    }
    /**
     * @Author milian
     * @Description //平台事务管理器
     * @Date 21:23 2021/9/13 0013
     * @Param [dataSource]
     * @return org.springframework.transaction.PlatformTransactionManager
     **/
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager ptm = new DataSourceTransactionManager();
        ptm.setDataSource(dataSource);
        return ptm;
    }
}
